package com.lxu.util;

import com.lxu.entity.WordEntity;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 纯段落占位符替换
 */
public class WordSimple1 {

    /**
     * 表格字段填充+尾部扩展行
     */
    @Test
    public void fillComplexWord4Tail() throws Exception {
        Map<String, Object> textMap = generateTextMap();
        WordEntity wordEntity = new WordEntity();
        wordEntity.setTextMap(textMap);
        /**
         * 填充占位符
         */
        InputStream in = this.getClass().getResourceAsStream("/templates/纯文本替换场景.docx");
        XWPFDocument xwpfDocument = XWPFUtils.fillComplexWord(in, wordEntity);
        XWPFUtils.save("D:\\ComplexWord.docx", xwpfDocument);
    }

    private Map<String, Object> generateTextMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "xuliang");
        map.put("age", "99");
        return map;
    }
}
